 
/**
 * Created by seeker910 on 2018/1/18.
 */
Rsd.define('Rsd.zen.dialog.MajorSubDialog', {
    extend: 'Rsd.container.Dialog',
    requires: ['Rsd.widget.MajorSubForm','Rsd.cqrs.MajorSub'],
    layout: 'vbox',
    items: [
        {
            xtype:'major-sub-form', 
            header:false,
            saveMajor:'saveMajor',
            deleteSubItem:'deleteSubItem',
            saveSubItem:'saveSubItem',
            flex:1,
        },
        {
            xtype:'container',
            header:false,
            height: 60, 
            margin:"0 10 0 0",
            layout:{type:'hbox', align:'right'},
            items: [ ]
        }
    ],
    /**
     * 
     */
    title:'无标题',
    //对接控件major-sub-form属性
    //major:{modelType:'',fields:[],keyName:''},
    //sub:{modelType:'',columns:[],fields:[],fkName:''},
     /**
      * 底部按钮
      */
     buttons:[],
     /**
      * 数据层和UI层交互处理服务
      */
     //handler:'cqrs-major-sub',
    /*
     * */
    constructor: function MajorSubDialog(config) {

        config = config || {};
        this.apply(config);
    },
    /**
     * 在初始化之前
    * */
    onBeforeInit:function onBeforeInit() {
        var me = this;
        this.menu = this.menu || {};
        //添加底部按钮
        var btns =  this.buttons||[]; 
        if(btns.length == 0)
        {
            this.items[1].hidden = true;
        }
        else
        {
            for(var i =0;i< btns.length;i++)
            {
                var _config = btns[i];
                if(Rsd.isString(btns[i]))
                {
                    _config = {text:btns[i]};
                }
                this.items[1].items.push(Rsd.apply({xtype:'button',height:40,margin: '0 10 0 10'},_config));
                if(this.items[1].items[i].xtype == 'button')
                {
                    this.items[1].items[i].fn = this.items[1].items[i].handler;
                    this.items[1].items[i].handler = function (e)
                    { 
                        this.funApplyByIOC(this.fn,[me.getData(),{parent:me,button:this,event:e}]);
                    }
                }
            }
        }
        //主从表单结构
        if (me.major.modelType) {

            setTimeout(function () {
                var _ms = me.items[0];  
                me.handler.getMajorSchema(_ms,function(){  }); 
            }, 10);
        }
        if (me.sub.modelType) {

            setTimeout(function () {
                var _ms = me.items[0];  
                me.handler.getSubSchema(_ms,function(){  }); 
            }, 10);
        }
        
        this.callParent(); 
        
    },
    
    /**
     * @description 加载主从表数据
     * */
    load:function load()
    {
        if(!this.major)
        {
            throw  new Error('Property \'major\' not found.');
        }
        if(!Rsd.isString(this.major.keyName))
        {
            throw  new Error('Property \'keyName\'  is undefined or not string value  in major.');
        }
        if(!Rsd.isString(this.sub.fkName))
        {
            throw  new Error('Property \'fkName\' is undefined or not string value in sub.');
        }

        var me = this;
       
        var _ms =  me.items[0];  
        if(!Rsd.isEmpty(me.data))
        {
            _ms.loadMajor(me.data);
            me.handler.listSub(_ms,function(){ }); 
        }    
        if(!Rsd.isEmpty(me.key))
        {
            //加载主表
            me.handler.getMajor(_ms,function(){  
                //加载从表
                me.handler.listSub(_ms,function(){ }); 
            }); 
        }
         
        return this;
    },

    /**
    * @description 仅加载主表数据
    *  */
    loadMajor:function loadMajor(row)
    {
        var _ms =  this.items[0];
        var _row = row||this.data;
        _ms.loadMajor(_row);
    },
    /**
    * @description 仅加载从表数据
    * */
    loadSub:function loadSub(list,callback)
    {
        var _ms =  this.items[0];
        _ms.loadSub(list||[],callback);
    },
     /**
     *  @description 获取数据
     */
     getData:function getData(target)
     {
         return this.items[0].getData(target);
     },
    /**
     * @description 在列表中一条记录
     * @param {*} row 
     * @param {*} editable 添加后表格状态是否可编辑
     * @param {*} index  记录添加位置，默认：在列表最后追加
     */
    addSub:function addSub(row,editable,index)
    {
        var _ms =  this.items[0];
        _ms.addSub(row,editable,index)
    },
    /**
     * @description 查看子表对象详细信息
     * @param {*} record 
     * @param {*} position 窗口打开位置,value is enum type:center or right or left
     */
    showSubItem:function showSubItem(record,position) {

        var _titleField = this.sub.titleField||'';
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(record ? record[_titleField] :'*');
        var _title = this.title +  '[' + _sub + ']';
        
        Rsd.app.showFormView({
            modelType:this.sub.modelType,
            formFields:this.sub.fields,
            data:record,
            title:_title,
            parent:this
        },position);
    },
   
    /**
     * @description 获取从表行数，含表头
     */
    getSubRowCount:function getSubRowCount()
    {
        return this.items[0].getSubRowCount();
    },
    /**
     * 
     */
    saveMajor:function saveMajor(callback)
    {
        var me = this;
        var _ms =  me.items[0];  
        me.handler.saveMajor(_ms,callback);  
    },
    /**
     * 
     */
    deleteSubItem:function deleteSubItem(row,evt,callback)
    {
        var me = this;
        var _ms =  me.items[0];  
        me.handler.deleteSub(_ms,row,callback);  
    },
    /**
      * 
      * @param {*} row 
      * @param {*} evt 
      */
    saveSubItem:function saveSubItem(row,evt,callback)
    { 
        var me = this;
        var _ms =  me.items[0];  
        me.handler.saveSub(_ms,row,callback);  
    }


},function (type) {

    var _subGetter = function () {
        var _ms =  this.items[0];

        return _ms.sub;

    };

    var _subSetter = function (value) {

        var _ms =  this.items[0];
        var _value = value || {};
        _ms.sub = Rsd.apply({modelType:'',fk:''},value || {});
        if(Rsd.isString(_value.header))
        {
            _value.header = {text:_value.header}; 
        }
        _ms.sub = Rsd.apply(_ms.sub,_value); 
        if(Rsd.isString(value.dataSource )) {
            _ms.sub.dataSource = Rsd.widget(value.dataSource);
        };


    }

    this.defineProperty(type,"sub", _subGetter, _subSetter,true);

    var _majorGetter = function () {
        var _ms =  this.items[0];

        return _ms.major;

    };

    var _majorSetter = function (value) {
        var _ms =  this.items[0];
        var _value = value || {};
        _ms.major = _ms.major||{key:'',titleField:''};
        if(Rsd.isString(_value.header))
        {
            _value.header = {text:_value.header}; 
        }
       
        _ms.major = Rsd.apply(_ms.major,_value); 
    }

    this.defineProperty(type,"major", _majorGetter, _majorSetter,true);


    var _handlerGetter = function () {
          
        if(!this.__handler)
        {
            this.__handler = Rsd.create('Rsd.cqrs.MajorSub',{owner:this});
        }
        return this.__handler;

    };

    var _handlerSetter = function (value) {

        if(Rsd.isString(value))
        {
            this.__handler = Rsd.widget(value,{owner:this});;
            return ;
        }
        this.__handler = value;
    }

    this.defineProperty(type,"handler", _handlerGetter, _handlerSetter,true);

    
});